﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PIM_ATE
{
    partial class FrmDeviceManage : Form
    {
        public FrmDeviceManage()
        {
            InitializeComponent();          
            m_textIPs = new List<TextBox>();
            m_textIPs.Add(text800);
            m_textIPs.Add(text900);
            m_textIPs.Add(text1800);
            m_textIPs.Add(text1900);
            m_textIPs.Add(text2100);
            m_textIPs.Add(text2600);
            m_textIPs.Add(text700);
            m_textIPs.Add(text400);
            m_textIPs.Add(text_rf1800);
            m_checkEnables = new List<CheckBox>();
            m_checkEnables.Add(check800);
            m_checkEnables.Add(check900);
            m_checkEnables.Add(check1800);
            m_checkEnables.Add(check1900);
            m_checkEnables.Add(check2100);
            m_checkEnables.Add(check2600);
            m_checkEnables.Add(check700);
            m_checkEnables.Add(check400);
            m_checkEnables.Add(check_rf1800);
            m_combTypes = new List<ComboBox>();
            m_combTypes.Add(combo800);
            m_combTypes.Add(combo900);
            m_combTypes.Add(combo1800);
            m_combTypes.Add(combo1900);
            m_combTypes.Add(combo2100);
            m_combTypes.Add(combo2600);
            m_combTypes.Add(combo700);
            m_combTypes.Add(combo400);
            m_combTypes.Add(combo_rf1800);
            m_bands = new List<string>();
            m_bands.Add("800");
            m_bands.Add("900");
            m_bands.Add("1800");
            m_bands.Add("1900");
            m_bands.Add("2100");
            m_bands.Add("2600");
            m_bands.Add("700");
            m_bands.Add("400");
            m_bands.Add("RF-1800");
        }

        List<TextBox> m_textIPs;
        List<CheckBox> m_checkEnables;
        List<ComboBox> m_combTypes;
        List<string> m_bands;

        private void FrmDeviceManage_Load(object sender, EventArgs e)
        {
            setEnable(false);          
            foreach (var d in JcPIMDevicesBuilder.dicDevices)
            {
                if (d.Value.instrBandAlias == "700")
                {
                    check700.Checked = d.Value.instrEnable;
                    text700.Text = d.Value.instrAddress;
                    combo700.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "800")
                {
                    check800.Checked = d.Value.instrEnable;
                    text800.Text = d.Value.instrAddress;
                    combo800.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "900")
                {
                    check900.Checked = d.Value.instrEnable;
                    text900.Text = d.Value.instrAddress;
                    combo900.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "1800")
                {
                    check1800.Checked = d.Value.instrEnable;
                    text1800.Text = d.Value.instrAddress;
                    combo1800.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "1900")
                {
                    check1900.Checked = d.Value.instrEnable;
                    text1900.Text = d.Value.instrAddress;
                    combo1900.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "2100")
                {
                    check2100.Checked = d.Value.instrEnable;
                    text2100.Text = d.Value.instrAddress;
                    combo2100.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "2600")
                {
                    check2600.Checked = d.Value.instrEnable;
                    text2600.Text = d.Value.instrAddress;
                    combo2600.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "400")
                {
                    check400.Checked = d.Value.instrEnable;
                    text400.Text = d.Value.instrAddress;
                    combo400.SelectedItem = d.Value.instrType.ToString();
                }
                else if (d.Value.instrBandAlias == "RF-1800")
                {
                    check_rf1800.Checked = d.Value.instrEnable;
                    text_rf1800.Text = d.Value.instrAddress;
                    combo_rf1800.SelectedItem = d.Value.instrType.ToString();
                }

            }
        }

        #region check

        private void check800_CheckedChanged(object sender, EventArgs e)
        {
            groupBox1.Enabled = check800.Checked;
        }

        private void check900_CheckedChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = check900.Checked;
        }

        private void check1800_CheckedChanged(object sender, EventArgs e)
        {
            //groupBox3.Enabled = check1800.Checked;
            if (check_rf1800.Checked)
                check1800.Checked = false;
        }

        private void check1900_CheckedChanged(object sender, EventArgs e)
        {
            groupBox4.Enabled = check1900.Checked;
        }

        private void check2100_CheckedChanged(object sender, EventArgs e)
        {
            groupBox5.Enabled = check2100.Checked;
        }

        private void check2600_CheckedChanged(object sender, EventArgs e)
        {
            groupBox6.Enabled = check2600.Checked;
        }

        private void check700_CheckedChanged(object sender, EventArgs e)
        {
            groupBox7.Enabled = check700.Checked;
        }

        private void check400_CheckedChanged(object sender, EventArgs e)
        {
            groupBox8.Enabled = check400.Checked;
        }

        private void check_rf1800_CheckedChanged(object sender, EventArgs e)
        {
            if (check1800.Checked)
                check_rf1800.Checked = false;
        }

        private void setEnable(bool enable)
        {
            check400.Checked = enable;
            check700.Checked = enable;
            check800.Checked = enable;
            check900.Checked = enable;
            check1800.Checked = enable;
            check1900.Checked = enable;
            check2100.Checked = enable;
            check2600.Checked = enable;
            check_rf1800.Checked = enable;
        }

        #endregion

        Instrument getType(string type)
        {
            switch (type)
            {
                case "DESKTOP_FOR_1REV": return Instrument.DESKTOP_FOR_1REV;
                case "DESKTOP_FOR_2REV": return Instrument.DESKTOP_FOR_2REV;
                case "DESKTOP_FOR_4REV": return Instrument.DESKTOP_FOR_4REV;
                case "DESKTOP_FOR_6REV": return Instrument.DESKTOP_FOR_6REV;
                case "DESKTOP_FOR_PORT": return Instrument.DESKTOP_FOR_PORT;
                default: return Instrument.DESKTOP_FOR_2REV;
            }
        }

        int getPort(Instrument instrType)
        {
            int instrPort = 2;
            if (instrType == Instrument.DESKTOP_FOR_FWD)
                instrPort = 1;
            else if (instrType == Instrument.DESKTOP_FOR_1REV)
                instrPort = 1;
            else if (instrType == Instrument.DESKTOP_FOR_2REV)
                instrPort = 2;
            else if (instrType == Instrument.DESKTOP_FOR_4REV)
                instrPort = 4;
            else if (instrType == Instrument.DESKTOP_FOR_6REV)
                instrPort = 6;
            else if (instrType == Instrument.DESKTOP_FOR_PORT)
                instrPort = 2;
            else if (instrType == Instrument.MULTI_BAND_FOR_HEIWEI)
                instrPort = 2;
            else if (instrType == Instrument.MULTI_BAND_FOR_HEIWEI_FWD)
                instrPort = 1;
            else if (instrType == Instrument.MULTI_BAND_FOR_POI)
                instrPort = 1;
            else if (instrType == Instrument.MULTI_BAND_FOR_NEWPOI)
                instrPort = 2;
            else if (instrType == Instrument.MULTI_BAND_FOR_NEWHEIWEI)
                instrPort = 2;
            else
                instrPort = 1;

            return instrPort;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (var d in JcPIMDevicesBuilder.dicDevices)
            {
                if (d.Value.instrBandAlias == "700")
                {
                    d.Value.instrEnable = check700.Checked;
                    d.Value.instrAddress = text700.Text;
                    d.Value.instrType = getType(combo700.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "800")
                {
                    d.Value.instrEnable = check800.Checked;
                    d.Value.instrAddress = text800.Text;
                    d.Value.instrType = getType(combo800.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "900")
                {
                    d.Value.instrEnable = check900.Checked;
                    d.Value.instrAddress = text900.Text;
                    d.Value.instrType = getType(combo900.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "1800")
                {
                    d.Value.instrEnable = check1800.Checked;
                    d.Value.instrAddress = text1800.Text;
                    d.Value.instrType = getType(combo1800.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "1900")
                {
                    d.Value.instrEnable = check1900.Checked;
                    d.Value.instrAddress = text1900.Text;
                    d.Value.instrType = getType(combo1900.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "2100")
                {
                    d.Value.instrEnable = check2100.Checked;
                    d.Value.instrAddress = text2100.Text;
                    d.Value.instrType = getType(combo2100.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "2600")
                {
                    d.Value.instrEnable = check2600.Checked;
                    d.Value.instrAddress = text2600.Text;
                    d.Value.instrType = getType(combo2600.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "400")
                {
                    d.Value.instrEnable = check400.Checked;
                    d.Value.instrAddress = text400.Text;
                    d.Value.instrType = getType(combo400.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
                else if (d.Value.instrBandAlias == "RF-1800")
                {
                    d.Value.instrEnable = check_rf1800.Checked;
                    d.Value.instrAddress = text_rf1800.Text;
                    d.Value.instrType = getType(combo_rf1800.Text);
                    d.Value.instrPort = getPort(d.Value.instrType);
                }
            }

            IniFile.SetFileName(Application.StartupPath + "\\JcDevices.ini");
            string section = "Desktop_Config";
            for (int i = 1; i <= 9; i++)
            {
                IniFile.IniWriteValue(section, "Device" + i + "_Band", m_bands[i - 1]);
                IniFile.IniWriteValue(section, "Device" + i + "_IP", m_textIPs[i - 1].Text);
                IniFile.IniWriteValue(section, "Device" + i + "_Type", m_combTypes[i - 1].Text);
                string value = m_checkEnables[i - 1].Checked ? "1" : "0";
                IniFile.IniWriteValue(section, "Device" + i + "_Enable  ", value);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnCacel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}
